package scct.hanyuesheng.dao;

import scct.hanyuesheng.model.User;
import scct.hanyuesheng.utils.DBUtils;

import javax.naming.Name;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//封装了对user表的数据库操作
public class UserDao {
    public User Login(String name, String pass) throws SQLException {
        User user = null;
        Connection conn = DBUtils.getConnection();
        String sql = "select* from sm_user where name=? and pass=?";
//      编译查询对象
        try {
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, name);
        pstmt.setString(2, pass);
//      查询
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            int id = rs.getInt("id");
            String username = rs.getString("name");
            String password = rs.getString("pass");
            String gender = rs.getString("gender");
            int age = rs.getInt("age");
            String mobile = rs.getString("mobile");
            user  = new User(id, username, password, gender, age, mobile);

        }
        DBUtils.closeConnection(conn);

    }catch(SQLException e){
        throw new RuntimeException(e);
    }
        return user;
    }
    public boolean updateUser(int id,  String password, String gender, int age, String mobile){
        int count=0;
        Connection conn=DBUtils.getConnection();
        String update_sql="update sm_user set mobile=?,age=?,gender=?,pass=? where id=?";
        try{
            PreparedStatement pstmt=conn.prepareStatement(update_sql);
            pstmt.setString(1, mobile);
            pstmt.setInt(2, age);
            pstmt.setString(3, gender);
            pstmt.setString(4, password);
            pstmt.setInt(5, id);

            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count > 0;
    }

//    判断用户名是否存在
    public boolean isDupname(String name) {
        int count = 0;//查找到的用户数量
        Connection conn = DBUtils.getConnection();
        String sql = "select * from sm_user where name=?";
        try {
        PreparedStatement pstm = conn.prepareStatement(sql);
        pstm.setString(1, name);
        ResultSet rs = pstm.executeQuery();
        if (rs.next()) {
            count = 1;
        }
    }catch (Exception e){
            throw new RuntimeException(e);
        }
    return count>0;
    }

//    添加用户方法
    public List<User> getAllUser()  {
        Connection conn=DBUtils.getConnection();
        List<User> users=new ArrayList<>();

        String sql="select * from sm_user";

        try {
            PreparedStatement pstmt=conn.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                int id=rs.getInt("id");
                String username=rs.getString("name");
                String password=rs.getString("pass");
                String gender=rs.getString("gender");
                int age=rs.getInt("age");
                String mobile=rs.getString("mobile");
                users.add(new User(id,username,password,gender,age,mobile));
            }
            DBUtils.closeConnection(conn);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        return users;
    }

    public boolean addUser(String username,String password,String gender,int age,String mobile){
        int result=0;
        Connection conn=DBUtils.getConnection();
        String insert_sql="insert into sm_user values(null,?,?,?,?,?)";
        try {
            PreparedStatement pstmt=conn.prepareStatement(insert_sql);
            pstmt.setString(1,username);
            pstmt.setString(2,password);
            pstmt.setString(3,gender);
            pstmt.setInt(4,age);
            pstmt.setString(5,mobile);
            result=pstmt.executeUpdate();

        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        return result==1;
    }

    public  User getUserById(int id){
        User user=null;
        Connection conn=DBUtils.getConnection();
        String sql="select* from sm_user where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);

            ResultSet rs=pstmt.executeQuery();
            if (rs.next()){
                String username= rs.getString("name");
                String password=rs.getString("pass");
                String mobile=rs.getString("mobile");
                String gender=rs.getString("gender");
                int age=rs.getInt("age");
                user=new User(id,username,password,gender,age,mobile);

            }
            DBUtils.closeConnection(conn);
        }catch (SQLException e){
            e.printStackTrace();

        }
        return user;
    }

}
